/* DownLoadServlet.java
 *
 * Project/Product: GWC_BS
 *
 * Modify Information:
 * =============================================================================
 *   Author Date       Description
 *   ---------- ---------- ---------------------------------------------------
 *   Administrator  2008-1-29   First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */
package system.imp;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import framework.core.*;
import system.base.*;

public class GWUpLoadServlet extends javax.servlet.http.HttpServlet {
   
    public GWUpLoadServlet()
    {
        super();
    }
    
    public void init() throws ServletException
    {
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws javax.servlet.ServletException, java.io.IOException
    {
       performTask(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws javax.servlet.ServletException, java.io.IOException
    {
        performTask(request, response);
    }

    public void performTask(HttpServletRequest req, HttpServletResponse res)
        throws javax.servlet.ServletException, java.io.IOException
    {
        
        SrvContext context = null;
        DBConnection con = null;
        String mReturn = "";
        
        try{
            context = new SrvContext(req, res); 
            con = DBConnection.open(false);
            con.setAutoCommit(false);
            String warehousecode = context.getString("warehouseCode");
            if (warehousecode == null) warehousecode = ""; 
            String tableName = context.getString("tableName");
            if (tableName == null) tableName = "";
            FileHolder upFile =  context.getFile("fileName");
            if(upFile != null)
            {               
                
                String fileName = upFile.getFileName();
                String strPath = req.getRealPath("/");
                if (!strPath.endsWith("\\"))
                {
                    strPath +="\\";
                }
                String strSavePath = ConstDefine.EXCELFILE_SAVEUPLOADFILE_PATH;
                System.out.println("===========================UploadFilePath:" + strPath + strSavePath + fileName);
                upFile.saveTo(strPath + strSavePath + fileName);
                
                String strFilePath = ConstDefine.EXCELFILE_SERVERUOLOADFILE_PATH + fileName;
                
                DataInputStream disfile = new DataInputStream(new java.io.ByteArrayInputStream(upFile.getBytes()));
                
                //保存Excel文件数据到数据库                
                //mReturn = GWFileUpProcessor.doExcelSaveToDB(disfile, con, null,tableName,warehousecode);
                mReturn = ExcelFileToDB.saveExcelFileToDB(disfile, con, strFilePath, tableName, warehousecode);
                disfile.close();

            }
            else
            {
                //文件为空,则返回:成功条数(0) + "-" + 失败条数(0) + "-" + 总条数(0)
                System.out.println("===========================UploadFilePath:No File" );
                mReturn = "0-0-0";
            }
            con.commit();
            
        }catch(Exception e)
        {
            try{
                System.out.println("GWUpLoadServlet.performTask:" + e.getMessage());
                con.rollback();
            }
            catch(Exception e1)
            {
                
            }
        }
        finally
        {
            try{
                if (con != null)
                {
                    con.close();
                }
            }
            catch(Exception e)
            {
                
            }
        }
        PrintWriter tOutput = res.getWriter();
        tOutput.print(mReturn);
    }
}
